home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / windows5 / winnet.zip / DUMP.C < prev    next >
C/C++ Source or Header  |  1990-06-25  |  5KB  |  248 lines

  1. /* comments are an admission that your code isn't clear enough */
  2.  
  3. /* History:62,1 0 */
  4. /* Fri Dec 15 11:08:53 1989 send_pkt was also dumping two bytes too many. 0 */
  5. /* Thu Dec 14 14:31:36 1989 receive_upcall was dumping another two bytes too many */
  6. /* Wed Aug 09 12:22:30 1989 receive_upcall was dumping two bytes too many */
  7.  
  8. #include <stdio.h>
  9. #include <ctype.h>
  10.  
  11. /* the format of the following struct is dependent upon trace.asm */
  12.  
  13. struct {
  14.     unsigned length;
  15.     unsigned char function;
  16.     unsigned long time;
  17.     unsigned char error;
  18.     union {
  19.         int handle;
  20.         unsigned char bytes[2000];
  21.         struct {
  22.             unsigned version;
  23.             unsigned char class;
  24.             unsigned type;
  25.             unsigned char number;
  26.             unsigned char basic;
  27.         } di;
  28.         struct {
  29.             unsigned char if_class;
  30.             unsigned if_type;
  31.             unsigned char if_number;
  32.             unsigned typelen;
  33.             unsigned handle;
  34.         } at;
  35.         struct {
  36.             unsigned handle;
  37.             unsigned length;
  38.         } ga;
  39.     } data;
  40. } record;
  41.  
  42. char *functions[] = {
  43.     "function_0",
  44.     "driver_info",
  45.     "access_type",
  46.     "release_type",
  47.     "send_pkt",
  48.     "terminate",
  49.     "get_address",
  50.     "reset_interface",
  51.     "function_8",
  52.     "function_9",
  53.     "get_parameters",
  54.     "as_send_pkt",
  55.     "function_12",
  56.     "function_13",
  57.     "function_14",
  58.     "function_15",
  59.     "function_16",
  60.     "function_17",
  61.     "function_18",
  62.     "function_19",
  63.     "set_rcv_mode",
  64.     "get_rcv_mode",
  65.     "set_multicast_list",
  66.     "get_multicast_list",
  67.     "get_statistics",
  68.     "set_address",
  69. };
  70.  
  71. enum {
  72.     bad_function,
  73.     driver_info,
  74.     access_type,
  75.     release_type,
  76.     send_pkt,
  77.     terminate,
  78.     get_address,
  79.     reset_interface,
  80.     set_rcv_mode,
  81.     get_rcv_mode,
  82.     set_multicast_list,
  83.     get_multicast_list,
  84.     get_statistics,
  85.     set_address,
  86.     receive_upcall = 255
  87. };
  88.  
  89. char *errors[] = {
  90.     "no_error",
  91.     "bad_handle",
  92.     "no_class",
  93.     "no_type",
  94.     "no_number",
  95.     "bad_type",
  96.     "no_multicast",
  97.     "cant_terminate",
  98.     "bad_mode",
  99.     "no_space",
  100.     "type_inuse",
  101.     "bad_command",
  102.     "cant_send",
  103.     "cant_set",
  104.     "bad_address",
  105. };
  106.  
  107. main()
  108. {
  109.     FILE *inf;
  110.     static first_time = 1;
  111.     unsigned long time_zero;
  112.     char args[80], results[80];
  113.  
  114.     inf = fopen("trace.out", "rb");
  115.     if (!inf) {
  116.         fprintf(stderr, "dump: cannot open \"trace.out\"\n");
  117.         exit(1);
  118.     }
  119.  
  120.     setvbuf(stdout, NULL, _IOFBF, 4096);
  121.  
  122.     for(;;) {
  123.         if (fread(&record.length, 2, 1, inf) != 1)
  124.             break;
  125.         fread(&record.function, 1, record.length - 2, inf);
  126.         if (first_time) {
  127.             first_time = 0;
  128.             time_zero = record.time;
  129.         }
  130.         args[0] = '\0';
  131.         switch(record.function) {
  132.         case get_statistics:
  133.         case terminate:
  134.         case reset_interface:
  135.         case release_type:
  136.         case get_address:
  137.         case receive_upcall:
  138.             sprintf(args, "%d", record.data.handle);
  139.             break;
  140.         case access_type:
  141.             sprintf(args, "%d %d %d %d", record.data.at.if_class,
  142.                 record.data.at.if_type,
  143.                 record.data.at.if_number,
  144.                 record.data.at.typelen);
  145.             break;
  146.         default:
  147.             strcpy(args, "");
  148.         }
  149.         if (record.error != 0) {
  150.             strcpy(results, errors[record.error]);
  151.         } else switch(record.function) {
  152.         case driver_info:
  153.             sprintf(results, "%d %d %d %d %d",
  154.                 record.data.di.version, record.data.di.class,
  155.                 record.data.di.type, record.data.di.number,
  156.                 record.data.di.basic);
  157.             break;
  158.         case access_type:
  159.             sprintf(results, "%d", record.data.at.handle);
  160.             break;
  161.         case send_pkt:
  162.             sprintf(results, "%d bytes", record.length - 8);
  163.             break;
  164.         case receive_upcall:
  165.             sprintf(results, "%d bytes", record.length - 10);
  166.             break;
  167.         default:
  168.             strcpy(results, errors[0]);
  169.         }
  170.         printf("%7.2f %s(%s) = %s\n",
  171.             (float)(record.time - time_zero) / 18.2,
  172.             record.function == receive_upcall?"receive_upcall":
  173.             functions[record.function], args, results);
  174.         switch(record.function) {
  175.         case get_address:
  176.             dump_bytes(&record.data.bytes[4], record.data.ga.length);
  177.             break;
  178.         case receive_upcall:
  179.             dump_bytes(&record.data.bytes[2], record.length - 10);
  180.             break;
  181.         case send_pkt:
  182.         case set_rcv_mode:
  183.             dump_bytes(&record.data.bytes[0], record.length - 8);
  184.             break;
  185.         }
  186.     }
  187. }
  188.  
  189. dump_bytes(unsigned char *bytes, int count)
  190. {
  191.     int n;
  192.     char buf[16];
  193.     int address;
  194.     void fmtline();
  195.  
  196.     address = 0;
  197.     while(count){
  198.         if (count > 16) n = 16;
  199.         else n = count;
  200.         fmtline(address,bytes,n);
  201.         address += n;
  202.         count -= n;
  203.         bytes += n;
  204.     }
  205. }
  206. /* Print a buffer up to 16 bytes long in formatted hex with ascii
  207.  * translation, e.g.,
  208.  * 0000: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f  0123456789:;<=>?
  209.  */
  210. void
  211. fmtline(addr,buf,len)
  212. int addr;
  213. char *buf;
  214. int len;
  215. {
  216.     char line[80];
  217.     register char *aptr,*cptr;
  218.     unsigned register char c;
  219.     void ctohex();
  220.  
  221.     memset(line,' ',sizeof(line));
  222.     ctohex(line,addr >> 8);
  223.     ctohex(line+2,addr & 0xff);
  224.     aptr = &line[6];
  225.     cptr = &line[55];
  226.     while(len-- != 0){
  227.         c = *buf++;
  228.         ctohex(aptr,c);
  229.         aptr += 3;
  230.         c &= 0x7f;
  231.         *cptr++ = isprint(c) ? c : '.';
  232.     }
  233.     *cptr++ = '\n';
  234.     fwrite(line,1,(unsigned)(cptr-line),stdout);
  235. }
  236. /* Convert byte to two ascii-hex characters */
  237. static
  238. void
  239. ctohex(buf,c)
  240. register char *buf;
  241. register int c;
  242. {
  243.     static char hex[] = "0123456789abcdef";
  244.  
  245.     *buf++ = hex[c >> 4];
  246.     *buf = hex[c & 0xf];
  247. }
  248.